<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*深拷贝：深拷贝拷贝的是地址 */

    /*
    深拷贝的实现方法一：JSON.parse(JSON.stringify(被拷贝的对象))
    const object1 = { name: "大陶品奇", age: 22, children: { value: "小陶品奇" } }
    const object2 = JSON.parse(JSON.stringify(object1))
    object2.name = "大马云"
    object2.children.value = "小马云"
    console.log(object1, object2);
    */



    //深拷贝的实现方法二：递归
    const object1 = { name: "大陶品奇", age: 22, children: { value: "小陶品奇" } }
    const object2 = {}
    function deepCopy(newObject, oldObject) {
      for (const key in oldObject) {
        if (oldObject[key] instanceof Array) {
          newObject[key] = []
          deepCopy(newObject[key], oldObject[key])
        } else if (oldObject[key] instanceof Object) {
          newObject[key] = {}
          deepCopy(newObject[key], oldObject[key])
        } else {
          newObject[key] = oldObject[key]
        }
      }
    }
    deepCopy(object2, object1)


    //深拷贝的实现方法三：lodash里面的_.cloneDeep()函数
    //语法： const newObject=_.cloneDeep(要被拷贝的对象)

  </script>
</body>

</html>